﻿using Microsoft.AspNetCore.Http;
using Models.Common;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using UtilsORM;
using UtilsORM.MySqlData;

////using System.Web.Script.Serialization;

namespace ServiceWeb.DBGateway
{
    public class DBIotDeviceCommand
    {


        EFYihongIotContext ef = new EFYihongIotContext();
        public List<iot_device_command> getCommandList(string guid)
        {
            return ef.iot_device_command.Where(c => c.dc_model_guid == guid && c.dc_status == 0).ToList();
        }
        public List<iot_device_command> getCommandList(string guid,int type)
        {
            return ef.iot_device_command.Where(c => c.dc_model_guid == guid && c.dc_status == 0 && c.dc_key_type == type).ToList();
        }
        public iot_device_command getCommand(string guid)
        {
            return ef.iot_device_command.Where(c => c.dc_guid == guid).FirstOrDefault();
        }

        public List<iot_device_command> getCommandByModel(string mguid)
        {
            return ef.iot_device_command.Where(c => c.dc_model_guid == mguid).ToList();
        }

        public List<iot_device_command> getCommandByModeBytype(string mguid, int dc_key_type)
        {
            return ef.iot_device_command.Where(c => c.dc_model_guid == mguid && c.dc_key_type == dc_key_type).ToList();
        }

        public bool saveAll(IFormCollection FC, HttpReqModel httpRequest, MagerLoginInfo LoginUserInfo, iot_device_command req)
        {

            //先查一下有没重复提交
            iot_device_command model = ef.iot_device_command.Where(c => c.dc_guid == req.dc_guid).FirstOrDefault();
            if (model == null)
            {
                //设置order
                //查这个设备里有没有
                if (req.dc_order == null)
                {
                    iot_device_command existCommand = ef.iot_device_command.Where(c => c.dc_model_guid == req.dc_model_guid && c.dc_order != null).OrderByDescending(c => c.dc_order).FirstOrDefault();
                    //查编码
                    if (existCommand == null)
                    {
                        req.dc_order = 1;
                    }
                    else
                    {
                        req.dc_order = existCommand.dc_order + 1;
                    }
                }

                //写入
                req.dc_guid = Guid.NewGuid().ToString();
                req.dc_agreement_type = ef.iot_device_agreement.Where(c => c.a_guid == req.dc_agreement_guid).FirstOrDefault().a_name;
                req.dc_add_time = DateTime.Now;
                ef.iot_device_command.Add(req);
                ef.SaveChanges();
            }
            else
            { //  1执行器，2传感器，3网关,4延时
                model.dc_name = req.dc_name;
                model.dc_key = req.dc_key;
                model.dc_content = req.dc_content;
                model.dc_agreement_type = ef.iot_device_agreement.Where(c => c.a_guid == req.dc_agreement_guid).FirstOrDefault().a_name;
                model.dc_agreement_guid = req.dc_agreement_guid;
                model.dc_key_type = req.dc_key_type;
                model.dc_add_time = DateTime.Now;
                ef.iot_device_command.Update(model);
                ef.SaveChanges();
            }
            return true;
        }
    }
}
